@echo off
echo Building and testing Enhanced Audit Trail System...
echo ================================================

cd /d "%~dp0"

echo.
echo === Building Main Application ===
echo Cleaning previous build...
if exist "AuditTrailSystem.exe" del "AuditTrailSystem.exe"
if exist "Makefile" del "Makefile"

echo Running qmake for main application...
qmake AuditTrailSystem.pro
if %ERRORLEVEL% neq 0 (
    echo qmake failed for main application!
    pause
    exit /b 1
)

echo Building main application...
mingw32-make clean
mingw32-make

if %ERRORLEVEL% neq 0 (
    echo Main application build failed!
    pause
    exit /b 1
)

echo Main application built successfully!

echo.
echo === Building Demo Application ===
cd build

echo Cleaning previous demo build...
if exist "demo.exe" del "demo.exe"
if exist "Makefile" del "Makefile"

echo Running qmake for demo application...
qmake demo.pro
if %ERRORLEVEL% neq 0 (
    echo qmake failed for demo application!
    cd ..
    pause
    exit /b 1
)

echo Building demo application...
mingw32-make clean
mingw32-make

if %ERRORLEVEL% neq 0 (
    echo Demo application build failed!
    cd ..
    pause
    exit /b 1
)

echo Demo application built successfully!

echo.
echo === Building Test Application ===
echo Cleaning previous test build...
if exist "test.exe" del "test.exe"
if exist "Makefile" del "Makefile"

echo Running qmake for test application...
qmake test.pro
if %ERRORLEVEL% neq 0 (
    echo qmake failed for test application!
    cd ..
    pause
    exit /b 1
)

echo Building test application...
mingw32-make clean
mingw32-make

if %ERRORLEVEL% neq 0 (
    echo Test application build failed!
    cd ..
    pause
    exit /b 1
)

echo Test application built successfully!

cd ..

echo.
echo === Build Summary ===
echo Checking built executables...

if exist "AuditTrailSystem.exe" (
    echo [✓] Main Application: AuditTrailSystem.exe
) else (
    echo [✗] Main Application: MISSING
)

if exist "build\demo.exe" (
    echo [✓] Demo Application: build\demo.exe
) else (
    echo [✗] Demo Application: MISSING
)

if exist "build\test.exe" (
    echo [✓] Test Application: build\test.exe
) else (
    echo [✗] Test Application: MISSING
)

echo.
echo === Running Applications ===
echo Starting main application...
if exist "AuditTrailSystem.exe" (
    start "" "AuditTrailSystem.exe"
    echo Main application started.
) else (
    echo Main application not found!
)

echo.
echo Starting demo application...
if exist "build\demo.exe" (
    start "" "build\demo.exe"
    echo Demo application started.
) else (
    echo Demo application not found!
)

echo.
echo Starting test application...
if exist "build\test.exe" (
    start "" "build\test.exe"
    echo Test application started.
) else (
    echo Test application not found!
)

echo.
echo ================================================
echo Enhanced Audit Trail System Build Complete!
echo ================================================
echo.
echo Features implemented:
echo - Three-tier permission management (Viewer/Operator/Admin)
echo - Advanced data export (CSV, JSON, XML, PDF, Excel, HTML)
echo - Plugin system for external program integration
echo - Comprehensive audit logging and tracking
echo.
echo Check the running applications for functionality verification.
echo Press any key to exit...
pause >nul